package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.pro.domain.dao.ActivityCorpDao;
import com.xbongbong.pro.domain.entity.ActivityCorpEntity;
import com.xbongbong.saas.model.ActivityCorpModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @description: 公司公告记录
 * @Author: hongxiao
 * @date: 2019-03-25 17:37
 * @since v1.0
 */
@Service("activityCorpModel")
public class ActivityCorpModelImpl implements ActivityCorpModel {

    @Resource
    private ActivityCorpDao activityCorpDao;

    @Override
    public Integer insert(ActivityCorpEntity entity) {
        long now =DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return activityCorpDao.insert(entity);
    }

    @Override
    public Integer update(ActivityCorpEntity entity) {
        entity.setUpdateTime((long)DateUtil.getInt());
        return activityCorpDao.update(entity);
    }

    @Override
    public Integer save(ActivityCorpEntity entity) {
        if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
            return activityCorpDao.insert(entity);
        }
        return activityCorpDao.update(entity);
    }

    @Override
    public Integer deleteByKey(Long key) {
        return activityCorpDao.deleteByKey(key);
    }

    @Override
    public ActivityCorpEntity getByKey(Long key) {
        return activityCorpDao.getByKey(key);
    }

    @Override
    public List<ActivityCorpEntity> findEntitys(Map<String ,Object> param) {
        return activityCorpDao.findEntitys(param);
    }

    @Override
    public Integer getEntitysCount(Map<String ,Object> param) {
        return activityCorpDao.getEntitysCount(param);
    }

    @Override
    public Integer save(Long activityId, String corpid, String userId, String mobile, String platform){
        if(StringUtil.isEmpty(mobile)){
            mobile = "";
        }
        if(StringUtil.isEmpty(platform)) {
            platform = "";
        }
        ActivityCorpEntity entity;
        Map<String,Object> param = new HashMap<>(16);
        param.put("activityId", activityId);
        param.put("corpid", corpid);
        param.put("userId", userId);
        param.put("platform", platform);
        param.put("del", DelEnum.NORMAL.getDel());
        List<ActivityCorpEntity> list = findEntitys(param);
        if(list != null && !list.isEmpty()){
            entity = list.get(0);
        }else{
            entity = new ActivityCorpEntity(activityId, corpid, userId, mobile);
            entity.setPlatform(platform);
        }
        return save(entity);
    }
}
